package com.jinxin.platform.cdsideparkmgr.rpc;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jinxin.platform.base.common.annotation.RecEvent;
import com.jinxin.platform.base.common.pojo.JsonResult;
import com.jinxin.platform.base.common.support.PlatFormEventListener;
import com.jinxin.platform.cdsideparkmgr.bean.CdsideparkmgrParkRecord;
import com.jinxin.platform.cdsideparkmgr.bean.CdyardmgrCarInfo;
import com.jinxin.platform.cdsideparkmgr.common.Constant;
import com.jinxin.platform.cdsideparkmgr.service.ICdsideparkmgrCarInfoService;
import com.jinxin.platform.cdsideparkmgr.service.ICdsideparkmgrParkRecordService;
import com.jinxin.platform.cdsideparkmgr.utils.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * All rights Reserved, Designed By www.beonelot.com
 *
 * @version: V1.0
 * @Copyright: www.beonelot.com Inc. All rights reserved.
 * @Description：
 * @Date: 2021-02-05
 * @Author: Lingnan
 **/
@Component
@RecEvent(eventType = "reportDistinguish")
@Slf4j
public class ParkRecordReportListener implements PlatFormEventListener {
    @Autowired
    private ICdsideparkmgrCarInfoService carInfoService;
    @Autowired
    private ICdsideparkmgrParkRecordService parkRecordService;

    @Override
    public JsonResult event(Map resultMap) {
        //{"data":{"reportMessage":"{\"r2\":\"00\",\"r3\":\"01\",\"r4\":\"00 98 \",\"r1\":\"02\"}",
        // "product_code":"00000a05","serial_num":"00000a0518110227","account":"1563700000001964",
        // "cmd_id":"4190","cmd_name":"Report"},"modules":["apcos-cqxfydjc"]}
        String upDataStr = JSON.toJSONString(resultMap);
        if(resultMap == null){return JsonResult.ok(ParkRecordReportListener.class);}
        if(!resultMap.containsKey("data") || !resultMap.containsKey("modules")){
            return JsonResult.ok(ParkRecordReportListener.class);
        }
        JSONObject upDataJson = JSONObject.parseObject(upDataStr);
        String modules = upDataJson.get("modules").toString();
        // 只处理自己模块的上报信息
        if(modules.contains(Constant.MODULE_KEY)){
            log.info("路边停车管理车辆识别上报: " + upDataStr);
            JSONObject dataJson = upDataJson.getJSONObject("data");
            JSONObject reportMessage = dataJson.getJSONObject("reportMessage");
            String carNum = reportMessage.getString("r9");
            String base64Pic = reportMessage.getString("r13");
            if(StringUtil.isEmpty(carNum)){
                return JsonResult.error(upDataStr);
            }
            String serialNum = dataJson.getString("serial_num");
            CdsideparkmgrParkRecord parkRecord = new CdsideparkmgrParkRecord();
            String province = carNum.substring(0, 1);
            String carNo = carNum.substring(1);
            HashMap param2 = new HashMap();
            param2.put("province",province);
            param2.put("carNo",carNo);
            CdyardmgrCarInfo carInfo = carInfoService.selectOne(param2);
            if(carInfo != null){
                parkRecord.setCategoryId(carInfo.getCategoryId());
                parkRecord.setCategory(carInfo.getCategory());
            }else {
                // 临停车
                parkRecord.setCategoryId(Constant.STRING_NUMER_ONE);
                parkRecord.setCategory(Constant.CAR_TYPE_TEMP);
            }
            JsonResult jsonResult = parkRecordService.saveOrUpdate(parkRecord);
            if(jsonResult.getSuccess()){
                log.info("车牌号("+carNum+")识别通过");
                return jsonResult;
            }
        }
        return JsonResult.error(upDataStr);
    }

}
